﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QuanLyNhaSach.Book
{
    public partial class FrmAuthorList : DevComponents.DotNetBar.Office2007Form
    {
        BindingList<AuthorDTO> listAuthorBinding;
        AuthorBUS authorBUS;
        RuleObjectBUS RuleBUS;
        int rowIndex; //Biến lưu giá trị của dòng đang sửa hoặc vừa thêm mới
        public FrmAuthorList()
        {
            InitializeComponent();
            dtgAuthorList.AutoGenerateColumns = false;
            authorBUS = new AuthorBUS();
            listAuthorBinding = new BindingList<AuthorDTO>();
            RuleBUS = new RuleObjectBUS();
            rowIndex = 0;
        }
        
        private void FrmAuthorList_Load(object sender, EventArgs e)
        {
            listAuthorBinding = authorBUS.GetNewBindingListAuthor();
            dtgAuthorList.DataSource = listAuthorBinding;
            this.VisibleChanged += new EventHandler(FrmAuthorList_VisibleChanged);
            CheckPermission();
        }

        void FrmAuthorList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                Global.GenerateNumber(dtgAuthorList, colNumber.Index);
                Global.GenerateEditColumn(dtgAuthorList, colEdit.Index);
                Global.GenerateDeleteColumn(dtgAuthorList, colDel.Index);
            }
        }

        private void dtgAuthorList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == colEdit.Index)
                {
                    UpdateAuthor(e.RowIndex);
                }
                else if (e.ColumnIndex == colDel.Index)
                {
                    DeleteAuthor();
                }
            }
        }
        public void AuthorChanged(AuthorDTO authorDto)
        {
            dtgAuthorList[colAuthorName.Index, rowIndex].Value = authorDto.AuthorName;
        }
        public void AuthorChangeAfterInsert(AuthorDTO authorDTO)
        {
            AuthorDTO authDTO = authorBUS.GetAuthorByID(authorDTO.AuthorID);

            listAuthorBinding.Add(authDTO);

            rowIndex = dtgAuthorList.Rows.GetLastRow(DataGridViewElementStates.None);
            dtgAuthorList[colEdit.Index, rowIndex].Value = Properties.Resources.edit_16;
            dtgAuthorList[colDel.Index, rowIndex].Value = Properties.Resources.deletered;
            if (rowIndex == 0)
            {
                dtgAuthorList[colNumber.Index, rowIndex].Value = rowIndex + 1;
            }
            else
            {
                dtgAuthorList[colNumber.Index, rowIndex].Value = dtgAuthorList[colNumber.Index, rowIndex - 1].Value.ToString();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DeleteAuthor();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dtgAuthorList.Rows.Count > 0)
            {
                UpdateAuthor(dtgAuthorList.CurrentRow.Index);
            }
        }

        private void UpdateAuthor(int index)
        {
            if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor))
            {
                Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                return;
            }

            AuthorDTO authorDTO = new AuthorDTO
            {
                AuthorID = int.Parse(dtgAuthorList[colAuthorID.Index, index].Value.ToString()),
                AuthorCode = dtgAuthorList[colAuthorCode.Index, index].Value.ToString(),
                AuthorName = dtgAuthorList[colAuthorName.Index, index].Value.ToString()
            };

            rowIndex = index;
            Book.FrmAuthorDetail frm = new FrmAuthorDetail { AuthorDto = authorDTO, Action = ActionName.Update };
            frm.ShowDialog();
        }

        private void DeleteAuthor()
        {
            if (dtgAuthorList.Rows.Count > 0)
            {
                if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor))
                {
                    Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                    return;
                }

                if (Global.ShowMessageBoxDelete("Bạn chắc chắn muốn xóa tác giả này ?") == DialogResult.Yes)
                {
                    int success = 0, faile = 0;
                    foreach (DataGridViewRow row in dtgAuthorList.SelectedRows)
                    {                        
                        int authorID = int.Parse(row.Cells[colAuthorID.Index].Value.ToString());
                        if (authorBUS.DeleteAuthor(authorID))
                        {
                            listAuthorBinding.Remove(listAuthorBinding.First(c => c.AuthorID == authorID));
                            success++;
                        }
                        else
                        {
                            faile++;
                        }
                    }

                    Global.SetMessage(lblMessage,string.Format("Xóa thành công : {0} dòng, thất bại : {1} dòng.",success,faile),true);
                }
            }
        }

        private void CheckPermission()
        {
            btnDel.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor);
            btnUpdate.Enabled = btnDel.Enabled;
        }
    }
}
